﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SmallFunctionTest
{
    public partial class SmallFunctionWindow : Form
    {
        public SmallFunctionWindow()
        {
            InitializeComponent();
        }

        private void btnGetMIMEType_Click(object sender, EventArgs e)
        {
            txtMIMEType.Text = getMimeType(txtFileName.Text);
        }

        private string getMimeType(string fileName)
        {
            string fileExtension = System.IO.Path.GetExtension(fileName).ToLower();

            try
            {
                var valueInTable = MimeTypeTable.MimeTypes.Value[fileExtension];
                return valueInTable as string;
            }
            catch (Exception)
            {
            }

            Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(fileExtension);
            if (rk != null && rk.GetValue("Content Type") != null)
                return rk.GetValue("Content Type").ToString();

            return null;
        }
    }
}
